/***********************************************************************************
 *FileName:  nsuc16xx_spi.c
 *Version:   V1.0
 *Date:      2024-04-22
 ****************************************Includes************************************/
#include "nsuc16xx_spi.h"

/***************************************Variables***********************************/

/***************************************Functions***********************************/
/**
 * @brief  Initializes the SPIx peripheral according to the specified
 *         parameters in the SPI_InitStruct.
 * @param  SPIx: Select the SPI peripheral.
 * @param  SPI_InitStruct: pointer to a SPI_InitTypeDef structure that
 *         contains the configuration information for the specified SPI peripheral.
 * @retval None
 */
void SPI_Init(SPI_TypeDef *SPIx, SPI_InitTypeDef *SPI_InitStruct)
{
    SPIx->CR_b.CPHA = SPI_InitStruct->SPI_CPHA;
    SPIx->CR_b.CPOL = SPI_InitStruct->SPI_CPOL;
    SPIx->CR_b.LSBF = SPI_InitStruct->SPI_FirstBit;
    SPIx->CR_b.DWID = SPI_InitStruct->SPI_DataSize;
    SPIx->CR_b.WMODE = SPI_InitStruct->SPI_WMode;
    SPIx->CR_b.SMODE = SPI_InitStruct->SPI_Direction;
    SPIx->CR_b.SPIC = SPI_InitStruct->SPI_BaudRatePrescaler;
    SPIx->CR_b.TXFCV = SPI_InitStruct->SPI_TXFIFOThreshold;
    SPIx->CR_b.RXFCV = SPI_InitStruct->SPI_RXFIFOThreshold;
}

/**
 * @brief  Enables or disables the specified SPI peripheral.
 * @param  SPIx: Select the SPI peripheral.
 * @param  NewState: new state of the SPIx peripheral.
 *   This parameter can be: ENABLE or DISABLE.
 * @retval None
 */
void SPI_Cmd(SPI_TypeDef *SPIx, FunctionalState NewState)
{
    if (NewState != DISABLE)
    {
        /* Enable the selected SPI peripheral */
        SPIx->CR_b.EN = 1;
    }
    else
    {
        /* Disable the selected SPI peripheral */
        SPIx->CR_b.EN = 0;
    }
}

/**
 * @brief  Set the SPI sending and receiving mode, which is valid only in 3-wire mode.
 * @param  SPIx: Select the SPI peripheral.
 * @param  Mode: SPI_Direction_TX or SPI_Direction_RX.
 * @retval None
 */
void SPI_SetTransceiverMode(SPI_TypeDef *SPIx, uint8_t Mode)
{
    SPIx->CR_b.SMODE = Mode;
}

/**
 * @brief  Transmits a Data through the SPIx/I2Sx peripheral.
 * @param  SPIx: Select the SPI peripheral.
 * @param  Data : Data to be transmitted.
 * @retval None
 */
void SPI_SendData(SPI_TypeDef *SPIx, uint32_t Data)
{
    SPIx->TXF = Data;
}

/**
 * @brief  Returns the most recent received data by the SPIx/I2Sx peripheral.
 * @param  SPIx: Select the SPI peripheral.
 * @retval The value of the received data.
 */
uint32_t SPI_ReceiveData(SPI_TypeDef *SPIx)
{
    return SPIx->RXF;
}

/**
 * @brief  Checks whether the specified SPI flag is set or not.
 * @param  SPIx: Select the SPI peripheral.
 * @param  SPI_FLAG: specifies the SPI/I2S flag to check.
 *   This parameter can be one of the following values:
 *     @arg SPI_FLAG_BUSY: Busy flag.
 *     @arg SPI_FLAG_TXFE: Transmit FIFO empty flag.
 *     @arg SPI_FLAG_TXFF: Transmit FIFO full flag.
 *     @arg SPI_FLAG_TXFC: Transmit FIFO reaches comparison flag
 *     @arg SPI_FLAG_TXFO: Transmit FIFO overflow flag.
 *     @arg SPI_FLAG_RXFE: Receive FIFO empty flag.
 *     @arg SPI_FLAG_RXFF: Receive FIFO full flag.
 *     @arg SPI_FLAG_RXFC: Receive FIFO reaches comparison flag
 *     @arg SPI_FLAG_RXFO: Receive FIFO overflow flag.
 * @retval The new state of SPI_I2S_FLAG (SET or RESET).
 */
FlagStatus SPI_GetFlagStatus(SPI_TypeDef *SPIx, uint32_t SPI_FLAG)
{
    FlagStatus bitstatus = RESET;
    if ((SPIx->SR & SPI_FLAG) != (uint16_t)RESET)
    {
        /* SPI_FLAG is set */
        bitstatus = SET;
    }
    else
    {
        /* SPI_FLAG is reset */
        bitstatus = RESET;
    }
    /* Return the SPI_FLAG status */
    return bitstatus;
}

/***************************************END OF FILE*********************************/
